package com.yyk.yykywd.mvvm.model

import com.yyk.yykywd.*
import com.yyk.yykywd.base.BaseResponse
import com.yyk.yykywd.bean.BaseBean
import com.yyk.yykywd.http.ApiService
import com.yyk.yykywd.mvvm.entity.FollowTypeBean
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * 文件描述：
 * Created by guo on 2019-12-02.
 */
class FollowModel {

    fun getType(getFollowType: GetFollowType) {
        val newMap = getNewMap()
        putSign(newMap)
        ApiService.getInstance().api.getChaseType(newMap)
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<FollowTypeBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: FollowTypeBean) {
                        getFollowType.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    fun saveFollow(type: String, id: String, followId: String, content: String, name: String, position: String,
                   phone: String, mid: String, m_type: String, m_text: String,
                   getBase: GetBase) {
        val newMap = getNewMap()
        putId(newMap, id)
        putUid(newMap, getUid())
        putTypeId(newMap, followId)
        putName(newMap, name)
        putPosition(newMap, position)
        putPhone(newMap, phone)
        if (mid.isBlank()) {
            putContent(newMap, content)
        } else {
            if (m_type == "1") {
                putContent(newMap, content)
            } else {
                putMText(newMap, m_text)
            }
            putMType(newMap, m_type)
            putMId(newMap, mid)
        }
        putSign(newMap)
        val observable: Observable<BaseBean> = if (type == FROM_POTENTIAL_CUSTOMER_HOSPITAL ||
                type == FROM_C_CUSTOMER_HOSPITAL ||
                type == FROM_SIGN_CONTRACT_HOSPITAL ||
                type == FROM_CUSTOMER_OPEN_SEA_HOSPITAL) {
            ApiService.getInstance().api.postHosChase(newMap)
        } else {
            ApiService.getInstance().api.postAgentChase(newMap)
        }
        observable
                .subscribeOn(Schedulers.io()) //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io()) //取消订阅
                .subscribe(object : Observer<BaseBean> {
                    override fun onComplete() {
                    }

                    override fun onSubscribe(d: Disposable?) {
                    }

                    override fun onNext(t: BaseBean) {
                        getBase.getInfo(t)
                    }

                    override fun onError(e: Throwable) {

                    }

                })
    }

    interface GetFollowType : BaseResponse {
        fun getInfo(type: FollowTypeBean)
    }
}